﻿using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Forms.Integration;
using Oxage.Simulator;

namespace Oxage.Controls
{
	public class RasterSimulationCanvas : Control, IStepDriver
	{
		#region Constructor
		public RasterSimulationCanvas()
		{
			this.DefaultStyleKey = typeof(RasterSimulationCanvas);
		}
		#endregion

		#region Template
		protected Grid RootElement;
		protected WindowsFormsHost host;
		protected RasterSimulationControl panel;

		public override void OnApplyTemplate()
		{
			RootElement = GetTemplateChild("RootElement") as Grid;
			host = GetTemplateChild("host") as WindowsFormsHost;
			panel = host.Child as RasterSimulationControl;

			base.OnApplyTemplate();
		}
		#endregion

		#region IStepDriver Members
		NPoint IStepDriver.Cursor
		{
			get
			{
				return this.cursor;
			}
		}
		private NPoint cursor = new NPoint();

		public IList<NPoint> ReferencePoints
		{
			get
			{
				return this.referencePoints;
			}
		}
		private List<NPoint> referencePoints = new List<NPoint>();

		public void Step(NPoint delta)
		{
			var d = delta.GetSignum();
			var driver = this as IStepDriver;
			foreach (var dim in d.GetKeys())
			{
				driver.Cursor[dim] += d[dim];
			}
			panel.Step((int)driver.Cursor.X, (int)driver.Cursor.Y, (int)driver.Cursor.Z);
		}
		#endregion
	}
}
